﻿Imports ReglasNegocio
Imports System.Type
Imports System.Data
Partial Public Class Login
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBox1.Focus()
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        EjecutarLogueo()
    End Sub

    Friend Sub EjecutarLogueo()
        Try
            Dim Catalogo As CatalogoPersona = New CatalogoPersona()
            Dim DsUsuario As DataSet = Catalogo.ObtenerUsuario(TextBox1.Text, TextBox2.Text)

            If DsUsuario.Tables(0).Rows.Count = 0 Then
                DisplayAlert("Login Incorrecto")
            Else
                Session("Usuario") = DsUsuario.Tables(0).Rows(0)("Usuario")
                Session("NomUsuario") = DsUsuario.Tables(0).Rows(0)("Apellido_Paterno") & " " & DsUsuario.Tables(0).Rows(0)("Apellido_materno") & " " & DsUsuario.Tables(0).Rows(0)("Nombres")
                Session("DNI") = DsUsuario.Tables(0).Rows(0)("DNI")

                If DsUsuario.Tables(0).Rows.Count = 1 Then
                    Session("Rol") = DsUsuario.Tables(0).Rows(0)("Rol")
                    Session("SistemaID") = DsUsuario.Tables(0).Rows(0)("Sistema")
                    Session("Sistema") = DsUsuario.Tables(0).Rows(0)("NomSistema")
                    Session("LocalID") = DsUsuario.Tables(0).Rows(0)("LocalID")
                    Session("Local") = DsUsuario.Tables(0).Rows(0)("Local")
                    FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, False)

                Else
                    LlenaCboSistema(TextBox1.Text)
                    LlenaCboRoles(TextBox1.Text, CboSistema.SelectedValue)
                    If CboRol.SelectedValue = "005" Then
                        LlenaCboLocal(TextBox1.Text, CboSistema.SelectedValue, CboRol.SelectedValue)
                    End If
                    ModalPopupExtender25.Show()

                End If


            End If

        Catch Ex As ClienteException
            Throw New ClienteException("Error Genérico", Ex)
        End Try
    End Sub


    Protected Sub BtnEntrar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEntrar.Click
        Session("SistemaID") = CboSistema.SelectedValue
        Session("Sistema") = CboSistema.SelectedItem.Text
        Session("Rol") = CboRol.SelectedValue

        If CboRol.SelectedValue = "005" Then
            Session("LocalID") = CboLocal.SelectedValue
            Session("Local") = CboLocal.SelectedItem.Text
        Else
            Session("LocalID") = ""
            Session("Local") = "-TODOS-"
        End If

        FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, False)

    End Sub

    Sub MiMensaje(ByVal Titulo As String, ByVal Mensaje As String, ByVal estilo As Integer)
        Dim msg As String
        Dim title As String
        Dim style As MsgBoxStyle
        msg = Mensaje
        style = estilo
        title = Titulo
        MsgBox(msg, style, title)
    End Sub


    Public Sub DisplayAlert(ByVal message As String)
        ClientScript.RegisterStartupScript(Me.GetType(), "Mensaje", "<script language=""JavaScript"">" & GetAlertScript(message) & "</script>")
    End Sub

    Public Function GetAlertScript(ByVal message As String) As String
        Return "alert('" & message.Replace("'", "\'") & "');"
    End Function


    Public Sub LlenaCboSistema(ByVal pstrUsuario As String)
        Dim i As Integer
        Dim ds As DataSet
        Dim item As ListItem
        Dim Catalogo As CatalogoPersona = New CatalogoPersona()
        ds = Catalogo.ObtenerCboSistema(pstrUsuario)
        CboSistema.Items.Clear()

        For i = 0 To ds.Tables(0).Rows.Count - 1
            item = New ListItem(ds.Tables(0).Rows(i).Item(1), ds.Tables(0).Rows(i).Item(0))
            CboSistema.Items.Add(item)
        Next

    End Sub



    Public Sub LlenaCboRoles(ByVal pstrUsuario As String, ByVal pstrSistema As String)
        Dim i As Integer
        Dim ds As DataSet
        Dim item As ListItem
        Dim Catalogo As CatalogoPersona = New CatalogoPersona()
        ds = Catalogo.ObtenerCboRol(pstrUsuario, pstrSistema)
        CboRol.Items.Clear()

        For i = 0 To ds.Tables(0).Rows.Count - 1
            item = New ListItem(ds.Tables(0).Rows(i).Item(1), ds.Tables(0).Rows(i).Item(0))
            CboRol.Items.Add(item)
        Next

    End Sub

    Protected Sub CboSistema_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboSistema.SelectedIndexChanged
        LlenaCboRoles(TextBox1.Text, CboSistema.SelectedValue)
        updatePanelRol.Update()
    End Sub

    Public Sub LlenaCboLocal(ByVal pstrUsuario As String, ByVal pstrSistema As String, ByVal pstrRol As String)
        Dim i As Integer
        Dim ds As DataSet
        Dim item As ListItem
        Dim Catalogo As CatalogoPersona = New CatalogoPersona()
        ds = Catalogo.ObtenerCboLocal(pstrUsuario, pstrSistema, pstrRol)
        CboLocal.Items.Clear()
        For i = 0 To ds.Tables(0).Rows.Count - 1
            item = New ListItem(ds.Tables(0).Rows(i).Item(1), ds.Tables(0).Rows(i).Item(0))
            CboLocal.Items.Add(item)
        Next
    End Sub

    Protected Sub CboRol_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboRol.SelectedIndexChanged
        LlenaCboLocal(TextBox1.Text, CboSistema.SelectedValue, CboRol.SelectedValue)
        updatePanelLocal.Update()
    End Sub

End Class